package com.css.zfzx.sjcj.modules.qhmapfiles.repository.impl;

import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.qhmapfiles.repository.QhMapFilesImport;
import com.css.zfzx.sjcj.modules.qhmapfiles.repository.QhMapFilesNativeRepository;
import com.css.zfzx.sjcj.modules.qhmapfiles.repository.entity.QhMapFilesEntity;
import com.css.zfzx.sjcj.modules.qhmapfiles.viewobjects.QhMapFilesQueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.*;

/**
 * @author zyt
 * @date 2022-04-19
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class QhMapFilesNativeRepositoryImpl implements QhMapFilesNativeRepository {
    @PersistenceContext
    private EntityManager em;

    @Value("${role.superCode}")
    private String superCode;
    @Value("${role.provinceCode}")
    private String provinceCode;
    @Value("${role.cityCode}")
    private String cityCode;
    @Value("${role.areaCode}")
    private String areaCode;

    private static final Logger log = LoggerFactory.getLogger(QhMapFilesNativeRepositoryImpl.class);

    @Autowired
    private QhMapFilesImport qhMapFilesImport;

    @Override
    public List<QhMapFilesEntity> searchMapFilesByJDBC(QhMapFilesQueryParams queryParams) {
        //List<QhMapFilesEntity> list = qhMapFilesImport.searchMapFiles(queryParams);
        String tableName = "qh_map_files";

        StringBuffer sql = new StringBuffer("select * from " + tableName + " ");

        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province like '%" + queryParams.getProvince() + "%'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city like '%" + queryParams.getCity() + "%'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area like '%" + queryParams.getArea() + "%'");
        } else {
            whereSql.append(" and (area IS NULL OR LENGTH(area)=0) ");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getAffiliatedFileName())) {
            whereSql.append(" and affiliated_file_name ='" + queryParams.getAffiliatedFileName() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsSave())) {
            whereSql.append(" and is_save ='" + queryParams.getIsSave() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsDelete())) {
            whereSql.append(" and is_delete = '" + queryParams.getIsDelete() + "'");
        }
        //上传类型（PED_,RLM_,RLMPolygon_,SCR_）
        if(!PlatformObjectUtils.isEmpty(queryParams.getType())) {
            whereSql.append(" and type = '" + queryParams.getType() + "'");
        }
        //上传省市级类型（0：省，1：市）
        if(!PlatformObjectUtils.isEmpty(queryParams.getQualityStatus())) {
            whereSql.append(" and quality_status = '" + queryParams.getQualityStatus() + "'");
        }
        //查询
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhMapFilesEntity.class);
        List<QhMapFilesEntity> list = query.getResultList();

        return list;
    }

    //查询所有未保存的文件
    @Override
    public List<QhMapFilesEntity> searchAllUnsaveMapFilesByJDBC() {
        StringBuilder sql = new StringBuilder("select * from qh_map_files ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        whereSql.append(" and is_save = '0' and is_delete = '0' ");//未保存未删除
        whereSql.append(" order by create_time desc ");//时间降序排列
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhMapFilesEntity.class);
        List<QhMapFilesEntity> list = query.getResultList();

        return list;
    }
}
